/**
 * Created by IntelliJ IDEA.
 * User: Tomek
 * Date: 24.06.12
 * Time: 13:11
 * To change this template use File | Settings | File Templates.
 */
package AS3 {
import mx.controls.Alert;

public class CountryFactory {

    private var countryXML:XML;

    public function CountryFactory(countryXMLData:XML) {
        this.countryXML = countryXMLData;
    }

    public function createCountry(countryName:String, region:int):Country {
        var country:Country = new Country(countryName, region);

        country.namePl = getCountryNamePl(countryName);

        var coalEnergy:Number = getCoalEnergy(countryName);
        var hydroEnergy:Number = getHydroEnergy(countryName);
        var oilEnergy:Number = getOilEnergy(countryName);
        var gasEnergy:Number = getGasEnergy(countryName);
        var nuclearEnergy:Number = getNuclearEnergy(countryName);


        // Alert.show("coalEnergy: " + coalEnergy);

        country.coalEnergy = coalEnergy;
        country.hydroEnergy = hydroEnergy;
        country.oilEnergy = oilEnergy;
        country.gasEnergy = gasEnergy;
        country.nuclearEnergy = nuclearEnergy;

        var population:Array = getPopulation(countryName);
        var temperatures:Array = getCountryTemperatures(countryName, false);
        var emissionPerCapita:Number = getEmissionPerCapita(countryName);
        var emissionPerCapitaEq:Number = getEmissionPerCapitaEq(countryName);
        var consumption2000:Number = getConsumption2000(countryName);
        var economyEfficiency:Number = getEconomyEfficiency(countryName);

        country.monthTemperatures = temperatures;
        country.emissionPerCapitaEq = emissionPerCapitaEq;
        country.emissionPerCapita = emissionPerCapita;
        country.populationArray = population;
        country.consumption2000 = consumption2000;
        country.economyEfficiency = economyEfficiency;


        return country;
    }

    private function getCountryNamePl(countryName:String):String {
        return countryXML.Continent.Country.(Name == countryName).Name_pl;
    }


    private function getCountryTemperatures(countryName:String, mainRegion:Boolean):Array {

        var temperatures:Array = new Array();
        var regionName:String = "CC";
        if (!mainRegion) {
            regionName = "CC";
        }
        var t_test:String = new String("");

        if (countryName != "" && countryName != null) {
            if (regionName == "CC") {
                t_test = countryXML.Continent.Country.(Name == countryName).Region.(Name_pl == regionName).T1;

                if (t_test != "") {
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name == regionName).T1);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name == regionName).T2);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name == regionName).T3);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name == regionName).T4);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name == regionName).T5);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name == regionName).T6);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name == regionName).T7);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name == regionName).T8);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name == regionName).T9);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name == regionName).T10);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name == regionName).T11);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name == regionName).T12);
                }
                else {
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name1 == regionName).T1);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name1 == regionName).T2);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name1 == regionName).T3);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name1 == regionName).T4);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name1 == regionName).T5);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name1 == regionName).T6);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name1 == regionName).T7);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name1 == regionName).T8);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name1 == regionName).T9);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name1 == regionName).T10);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name1 == regionName).T11);
                    temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name1 == regionName).T12);
                }

            }
            else {
                temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name_pl == regionName).T1);
                temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name_pl == regionName).T2);
                temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name_pl == regionName).T3);
                temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name_pl == regionName).T4);
                temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name_pl == regionName).T5);
                temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name_pl == regionName).T6);
                temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name_pl == regionName).T7);
                temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name_pl == regionName).T8);
                temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name_pl == regionName).T9);
                temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name_pl == regionName).T10);
                temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name_pl == regionName).T11);
                temperatures.push(countryXML.Continent.Country.(Name == countryName).Region.(Name_pl == regionName).T12);
            }
        }


        return temperatures;
    }

    private function getCoalEnergy(panstwo:String):Number {
        return countryXML.Continent.Country.(Name == panstwo).Coal;

    }

    private function getHydroEnergy(panstwo:String):Number {
        return countryXML.Continent.Country.(Name == panstwo).Hydro;

    }

    private function getOilEnergy(panstwo:String):Number {
        return countryXML.Continent.Country.(Name == panstwo).Oil;

    }

    private function getGasEnergy(panstwo:String):Number {
        return countryXML.Continent.Country.(Name == panstwo).Gas;

    }

    private function getNuclearEnergy(panstwo:String):Number {
        return countryXML.Continent.Country.(Name == panstwo).Nuclear;
    }

    private function getPopulation(panstwo:String):Array {
        var pop:Array = new Array();
        pop.push(countryXML.Continent.Country.(Name == panstwo).Pop0);
        pop.push(countryXML.Continent.Country.(Name == panstwo).Pop1);
        pop.push(countryXML.Continent.Country.(Name == panstwo).Pop2);
        pop.push(countryXML.Continent.Country.(Name == panstwo).Pop3);
        pop.push(countryXML.Continent.Country.(Name == panstwo).Pop4);
        pop.push(countryXML.Continent.Country.(Name == panstwo).Pop5);
        return pop;
    }

    private function getEmissionPerCapitaEq(panstwo:String):Number {
        return countryXML.Continent.Country.(Name == panstwo).EmissionPerCapitaEq;
    }

    private function getEmissionPerCapita(panstwo:String):Number {
        return countryXML.Continent.Country.(Name == panstwo).EmissionPerCapita;
    }

    private function getConsumption2000(panstwo:String):Number {
        return countryXML.Continent.Country.(Name == panstwo).Consumption2000;
    }

    private function getEconomyEfficiency(panstwo:String):Number {
        var EconomyEff:Number = new Number(0);
        EconomyEff = countryXML.Continent.Country.(Name == panstwo).EconomyEff;
        if (EconomyEff == 0) {
            EconomyEff = 1;
        }
        return EconomyEff;
    }


}
}
